import React from 'react';
import PropTypes from 'prop-types';
import Login from "./Login";
import QRCode from 'qrcode.react';
import axios from "axios/index";
import style from "../css/Project.css";
import styleMain from "../css/main.css";

class main extends React.Component {

    constructor(props, context) {
        super(props, context);
        this.uid = this.props.location.state.uid;
        this.GxToken = this.props.location.state.GxToken;
        this.GxSID = this.props.location.state.GxSID;
        this.state = {
            schoolList: [],
            tel:'',
            name_higherUp:'',
            tel_higherUp:'',
            name:''
        };


    }

    componentWillMount () {
        axios.defaults.withCredentials = true;
        axios.get('http://guoxue.boyueguoxue.cn/Channel/getInfoByUid.do?channelUid='+this.uid+'&browsGxToken='+this.GxToken        )
            .then(response => {
                let code=response.data.code;
                let message=response.data.message;
                console.log('code'+code);
                console.log(response.data.data);
                if(code==200){
                    let data=response.data.data;
                    this.setState({
                        tel:data.tel,
                        tel_higherUp:data.tel_higherUp,
                        schoolList:data.schoolList,
                        name_higherUp:data.name_higherUp,
                        name:data.name
                    })
                }else {
                    alert(message);
                    return;
                }
            })
            .catch(function (error) {
                alert(error)
            })
    }

    render () {
        let lists = this.state.schoolList.map((item,i) => (
            <div   key={i} >
                <div className={styleMain["main-div1"]}>
                <label className={styleMain["main-background"]} onClick={() => this.listClick(i)}>
                    {item.schoolName}
                    <div className={styleMain["main-divTriangle"]}>
                    <label className={styleMain["main-labelTriangle"]}></label>
                    </div>
                </label>

                </div>
            </div>
        ));

        return(
            <div>
                <div  className={styleMain["main-textCenter"]} >
                <a href="javascript:void(0);" onClick={() => this.my()}>个人中心</a>
                </div>
                <br/>
                <br/>
                <div className={styleMain["main-div"]}>
                <input className={styleMain["main-button"]} type="button" id="btn" value="点击创建新学校二维码" onClick={() => this.selectSchool()} />
                </div>
                    <br/>
                <div className={styleMain["main-list"]}>
                {lists}
                </div>
            </div>
        )
    }

    my() {
        alert(this.state.name);
        this.context.router.push({pathname:'/my',state:{userphones:this.state.tel,tui_phone:this.state.tel_higherUp,url:"http://oss0.boyueguoxue.cn/channel/index.html",name_higherUp:this.state.name}});
    }

    selectSchool() {
        this.context.router.push({pathname:'/selectSchool',state:{GxToken:this.GxToken}});
    }

    listClick(i) {
        this.context.router.push({pathname:'/ShowErwe/',state:{schoolName:this.state.schoolList[i].schoolName,wxTicket:this.state.schoolList[i].wxTicket,sceneId:this.state.schoolList[i].sceneId,GxToken:this.GxToken}});

        // console.log("----------------------"+this.state.schoolList[i]);
    }
}

main.contextTypes = {
    router: PropTypes.object.isRequired
};

export default main;